/*
 * MIT License
 *
 * Copyright (c) 2020 wen.gu <454727014@qq.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

 /***************************************************************************
 * Name: uds_transport.c
 *
 * Purpose: implementation implementation unix domain socket transport(dialer and listener) ops for cbus
 *
 * Developer:
 *   wen.gu , 2022-02-16
 *
 * TODO:
 *
 ***************************************************************************/

 /******************************************************************************
 **    INCLUDES
 ******************************************************************************/

#if defined(_MSC_VER)
#include "uds_transport.h"

#include "cbus/core/cbus_log.h"
/******************************************************************************
 **    MACROS
 ******************************************************************************/

/******************************************************************************
 **    VARIABLE DEFINITIONS
 ******************************************************************************/

/******************************************************************************
 **    inner FUNCTION DEFINITIONS
 ******************************************************************************/


/******************************************************************************
 **    FUNCTION DEFINITIONS
 ******************************************************************************/


/**
 * @brief if not NULL, is called once during library initialization
 * @param none
 * @return GErrc, success: G_OK, else error code.
 * 
*/
GErrc uds_transport_initialize()
{
    LOGD("[%s.%d]==>:\n", __FUNCTION__, __LINE__);
    
    static WSADATA wsaData;
    int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
    return (iResult == 0) ? G_OK : G_ErrUndefined;
}

/**
 * @brief if not NULL, is called during library deinitialization,
 *        It should release any global resources, close any open files, etc.
 * @param none
 * @return GErrc, success: G_OK, else error code.
 * 
 */
GErrc uds_transport_finish()
{
    WSACleanup();
    return G_OK;
}


//////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////

static const cbus_dialer_ops g_uds_dialer_ops = 
{
    uds_dialer_initialize,
    uds_dialer_finish,
    uds_dialer_connect,
    uds_dialer_disconnect,
    uds_dialer_send,
    uds_dialer_set_receiver
};

static const cbus_listener_ops g_uds_listener_ops = 
{
    uds_listener_initialize,
    uds_listener_finish,
    uds_listener_bind,
    uds_listener_unbind,
    uds_listener_send,
    uds_listener_set_receiver
};


static const cbus_transport_ops g_uds_trans_ops = 
{
    CBUS_TRANSPORT_OPS_VERSION,
    "ipc",
    uds_transport_initialize,
    uds_transport_finish,
    &g_uds_dialer_ops,
    &g_uds_listener_ops
};


GErrc cbus_uds_transport_default_ctor(void)
{
    return cbus_transport_register(&g_uds_trans_ops);
}





#endif